<?php
/**
 * 
 * @param number $classid
 * @param string $title
 * @param unknown $note
 * @param unknown $date
 * @param unknown $time
 * @return Ambigous <number, false, boolean, mixed>
 */
function seitcm_add_class_schedule($classid,$title,$note,$date,$time)
{
	global $wpdb;
	return $wpdb->query($wpdb->prepare('INSERT INTO ' . SEITCM_TABLE_SCHEDULE . ' (classid,title,note,time) VALUES (%d,%s,%s,%s,%s)',$classid,$title,$note,seitcm_create_datetime_for_mysql($date. ' ' .$time)));
}
/**
 * 
 * @param unknown $classid
 */
function seitcm_generate_schedule($classid)
{
	seitcm_remove_schedule($classid);
	$class_data = seitcm_get_single_class($classid);
	$coursesid = $class_data->coursesid;
	global $wpdb;
	$curriculumlist = seitcm_get_curriculum_for_courses($coursesid);
	$time = seitcm_get_default_schedule_time($class_data->learntime);
	$i=0;
	$wpdb->query('start transaction');
	foreach ($curriculumlist as $curriculum)
	{
		$date = $i==0?seitcm_create_date_from_mysql($class_data->startdate):seitcm_get_next_date_in_learntime($class_data->learntime, $date);
		$sql = $wpdb->prepare('INSERT INTO ' . SEITCM_TABLE_SCHEDULE . '(classid,title,time,note) VALUES (%d,%s,%s,%s)',$classid,$curriculum->title,seitcm_create_date_for_mysql2($date) . ' ' . $time,$curriculum->sumary);
		$i++;
		if(!($wpdb->query($sql)))
		{
			$wpdb->query('rollback');
			return false;
		}
	}
	$wpdb->query('commit');
	return true;
}

function seitcm_skip_schedule($id)
{
	global $wpdb;
	$current_schedule_data = seitcm_get_single_schedule($id);
	if($current_schedule_data->schedulestatus){
		$current_class_data = seitcm_get_single_class($current_schedule_data->classid);
		$current_class_schedule = seitcm_get_schedule_alist_after_day($current_schedule_data->classid,$current_schedule_data->time);
		$time = seitcm_get_default_schedule_time($current_class_data->learntime);
		$maxindex = count($current_class_schedule)-1;
		$wpdb->query('start transaction');
		$date = seitcm_create_date_from_mysql($current_class_schedule[$maxindex]->time);
		do{
			$date = seitcm_get_next_date_in_learntime($current_class_data->learntime, $date);
		}
		while (seitcm_check_is_skip_schedule($current_schedule_data->classid,seitcm_create_date_for_mysql($date) . ' ' . $time));
		$sql = $wpdb->prepare('INSERT INTO ' . SEITCM_TABLE_SCHEDULE . '(classid,title,time) VALUES (%d,%s,%s)',$current_class_schedule[$maxindex]->classid,$current_class_schedule[$maxindex]->title,seitcm_create_date_for_mysql($date) . ' ' . $time);
		if(!($wpdb->query($sql)))
		{
			$wpdb->query('rollback');
			return false;
		}
		for($i = $maxindex; $i>0; $i--){
			$sql = $wpdb->prepare('UPDATE ' . SEITCM_TABLE_SCHEDULE . ' SET title = %s,note = %s WHERE id = %d',$current_class_schedule[$i-1]->title,$current_class_schedule[$i-1]->note,$current_class_schedule[$i]->scheduleid);
			if(!($wpdb->query($sql)))
			{
				$wpdb->query('rollback');
				return false;
			}
		}
		$sql = $wpdb->prepare('UPDATE ' . SEITCM_TABLE_SCHEDULE . ' SET `status` = 0 WHERE id = %d',$id);
		if(!($wpdb->query($sql)))
		{
			$wpdb->query('rollback');
			return false;
		}
		$wpdb->query('commit');
		return true;
	}
}
/**
 * 
 * @param number $classid
 * @return Ambigous <boolean, number, false, mixed>
 */
function seitcm_remove_schedule($classid){
	global $wpdb;
	return seitcm_check_class_exist_schedule($classid)!=0?$wpdb->query($wpdb->prepare('DELETE FROM ' . SEITCM_TABLE_SCHEDULE . ' WHERE classid = %d',$classid)):true;
}
/**
 * 
 * @param number $classid
 * @return Ambigous <string, NULL>
 */
function seitcm_check_class_exist_schedule($classid){
	global $wpdb;
	return $wpdb->get_var($wpdb->prepare('SELECT count(id) FROM ' . SEITCM_TABLE_SCHEDULE . ' WHERE classid = %d',$classid));
}
/**
 *
 * @param number $classid
 * @return Ambigous <mixed, NULL, multitype:>
 */
function seitcm_get_last_schedule($classid){
	global $wpdb;
	return $wpdb->get_row($wpdb->prepare('SELECT * FROM '.SEITCM_TABLE_SCHEDULE.' WHERE classid = % d ORDER BY time DESC',$classid));
}

function seitcm_check_exist_date($classid,$date){
	global $wpdb;
	$result = $wpdb->get_results($wpdb->prepare('SELECT * FROM '.SEITCM_TABLE_SCHEDULE.' WHERE classid = % d ORDER BY time DESC',$classid));
	foreach ($result as $row)
	{
		if(seitcm_create_date_from_mysql($row->time) == $date) return true;
	}
	return false;
}
/**
 * 
 * @param number $classid
 * @return Ambigous <mixed, NULL, multitype:, multitype:multitype: , multitype:Ambigous <multitype:, NULL> >
 */
function seitcm_get_schedule_list($classid)
{
	global $wpdb;
	return $wpdb->get_results($wpdb->prepare('SELECT * FROM '.SEITCM_TABLE_SCHEDULE. ' WHERE classid = % d ORDER BY time',$classid));
}
/**
 * 
 * @param number $classid
 * @return Ambigous <mixed, NULL, multitype:, multitype:multitype: , multitype:Ambigous <multitype:, NULL> >
 */
function seitcm_get_schedule_list_after_day($classid,$datetime)
{
	global $wpdb;
	return $wpdb->get_results($wpdb->prepare('SELECT * FROM '.SEITCM_TABLE_SCHEDULE.' WHERE classid = % d AND time >= %s ORDER BY time',$classid,$datetime));
}
/**
 * 
 * @param number $classid
 * @return Ambigous <mixed, NULL, multitype:, multitype:multitype: , multitype:Ambigous <multitype:, NULL> >
 */
function seitcm_get_schedule_alist_after_day($classid,$datetime)
{
	global $wpdb;
	return $wpdb->get_results($wpdb->prepare('SELECT * FROM '.SEITCM_TABLE_SCHEDULE.' WHERE classid = % d AND time >= %s AND status = 1 ORDER BY time',$classid,$datetime));
}
/**
 * 
 * @param unknown $id
 * @return Ambigous <mixed, NULL, multitype:>
 */
function seitcm_get_single_schedule($id)
{
	global $wpdb;
	return $wpdb->get_row($wpdb->prepare('SELECT * FROM '.SEITCM_TABLE_SCHEDULE.' WHERE id = % d',$id));
}
/**
 *
 * @param string $learntimekey
 * @return string
 */
function seitcm_get_default_schedule_time($learntimekey)
{
	global $seitcm_learntime;
	foreach ($seitcm_learntime as $learntimes)
	{
		if($learntimes['key'] == $learntimekey)
		{
			$duration = $learntimes['duration'];
		}
	}
	$parts = explode('h', $duration);
	if($parts[0]>9)
		return $parts[0].':'.$parts[1].':00';
	else return '0'.$parts[0].':'.$parts[1].':00';	
}
/**
 * COUNT number session of a class 
 * @param string $learntimekey
 * @return string
 */
function seitcm_get_count_schedule_of_class($classid)
{
	global $wpdb;
	return $wpdb->get_var($wpdb->prepare('SELECT COUNT(id) FROM '. SEITCM_TABLE_SCHEDULE .' WHERE classid = %d',$classid));
}
/**
 *
 * @param int $learntime
 * @param string $curentdatestring
 * @return string
 */
function seitcm_get_next_date_in_learntime($learntime,$curentdatestring)
{
	global $seitcm_learntime;
	$dateparts = explode('/', $curentdatestring);
	$found=false;
	do {
		$next_date = date('d/m/Y',mktime(0, 0, 0, $dateparts[1], ++$dateparts[0], $dateparts[2]));
		if(seitcm_check_date_is_in_learntime($learntime,$next_date))
		{
			$found=true;
		}
	}
	while ($found==false);
	return $next_date;
}
/**
 * 
 * @param number $classid
 * @return string
 */
function seitcm_get_next_date_for_schedule($classid)
{
	$scheduleobject = seitcm_get_last_schedule($classid);
	if(count($scheduleobject)==0)
	{
		return seitcm_create_date_from_mysql(seitcm_get_single_class($classid)->startdate);
	}
	else
	{
		$currentdate = $scheduleobject->time;
		$currentdate = seitcm_create_date_from_mysql($currentdate);
		return seitcm_get_next_date_in_learntime(seitcm_get_single_class($classid)->learntime,$currentdate);
	}
}
/**
 * 
 * @param number $classid
 * @param string $datetime
 * @return Ambigous <string, NULL>
 */
function seitcm_check_is_skip_schedule($classid,$datetime)
{
	global $wpdb;
	$sql = $wpdb->prepare('SELECT status FROM ' . SEITCM_TABLE_SCHEDULE . ' WHERE classid = %d AND time = %s',$classid,$datetime);
	return $wpdb->get_var($sql);
}
/**
 * 
 * @param number $classid
 * @return Ambigous <mixed, NULL, multitype:>
 */
function seitcm_get_current_schedule_for_class($classid)
{
	global $wpdb;
	$sql = $wpdb->prepare('SELECT * FROM ' . SEITCM_TABLE_SCHEDULE . ' WHERE classid = %d AND DATE_FORMAT(`time`,\'%s\') = DATE_FORMAT(\'%s\',\'%s\')',$classid,'%Y-%m-%d',seitcm_get_current_sql_date(0,0,0),'%Y-%m-%d');
	return $wpdb->get_row($sql);
}
/**
 * 
 * @param unknown $classid
 * @return Ambigous <string, NULL>
 */
function seitcm_check_haven_schedule_now($classid){
	 $schedule = seitcm_get_current_schedule_for_class($classid);
	 if($schedule)
	 	return $schedule->status;
	 return false;
}
function seitcm_check_schedule_today($scheduleid){
	$schedule = seitcm_get_single_schedule($scheduleid);
	if(seitcm_create_date_from_mysql($schedule->time) == seitcm_get_current_date())
		return $schedule->status;
	return false;
}
function seitcm_check_schedule_over($scheduleid){
	$schedule = seitcm_get_single_schedule($scheduleid);
	if(!seitcm_compare_date(seitcm_create_date_from_mysql($schedule->time),seitcm_get_current_date(0,0,0)))
		return $schedule->status;
	return false;
}
/**
 * 
 * @param unknown $id
 * @param unknown $title
 * @param unknown $note
 * @return Ambigous <number, false, boolean, mixed>
 */
function seitcm_update_schedule($id,$title,$note)
{
	global $wpdb;
	$sql = $wpdb->prepare('UPDATE ' . SEITCM_TABLE_SCHEDULE . ' SET title = %s,note = %s WHERE id = %d',$title,$note,$id);
	return $wpdb->query($sql);
}
?>